package com.wenjie.service.impl;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class CodeTemplateTools {


    /**
     * @Description 将数据库类型转换为Java类型
     * @author wenjie
     * @date 2024/8/21 15:17
     */
    public static String db_to_java(String value) {
        String dbType = value.trim();
        String javaType;
        if ("int".equalsIgnoreCase(dbType) || "integer".equalsIgnoreCase(dbType)) {
            javaType = "Integer";
        } else if ("bigint".equalsIgnoreCase(dbType)) {
            javaType = "Long";
        } else if ("smallint".equalsIgnoreCase(dbType)) {
            javaType = "Short";
        } else if ("tinyint".equalsIgnoreCase(dbType)) {
            javaType = "Byte";
        } else if ("float".equalsIgnoreCase(dbType)) {
            javaType = "float";
        } else if ("double".equalsIgnoreCase(dbType)) {
            javaType = "double";
        } else if ("decimal".equalsIgnoreCase(dbType) || "numeric".equalsIgnoreCase(dbType)) {
            javaType = "java.math.BigDecimal";
        } else if ("char".equalsIgnoreCase(dbType)) {
            javaType = "String";
        } else if ("varchar".equalsIgnoreCase(dbType) || "text".equalsIgnoreCase(dbType)) {
            javaType = "String";
        } else if ("date".equalsIgnoreCase(dbType)) {
            javaType = "java.sql.Date";
        } else if ("time".equalsIgnoreCase(dbType)) {
            javaType = "java.sql.Time";
        } else if ("timestamp".equalsIgnoreCase(dbType)) {
            javaType = "java.sql.Timestamp";
        } else if ("blob".equalsIgnoreCase(dbType)) {
            javaType = "byte[]";
        } else if ("clob".equalsIgnoreCase(dbType)) {
            javaType = "String";
        } else {
            javaType = "Unknown Type";
        }

        return javaType;
    }


    /**
     * @Description 首字母大写
     * @author wenjie
     * @date 2024/8/21 15:21
     */
    public static String first_big(String value) {
        if (value == null || value.isEmpty()) return value;
        return value.substring(0, 1).toUpperCase() + value.substring(1);
    }


    /**
     * @Description 首字母小写
     * @author wenjie
     * @date 2024/8/21 15:32
     */
    public static String first_small(String value) {
        if (value == null || value.isEmpty()) return value;
        return value.substring(0, 1).toLowerCase() + value.substring(1);
    }


    /**
     * @Description _将其设置为首字母大写的形式
     * @author wenjie
     * @date 2024/8/21 15:28
     */
    public static String class_to_java(String s) {
        if (s == null) {
            return null;
        }
        StringBuilder camelCaseStr = new StringBuilder();
        String[] parts = s.split("_");
        for (String part : parts) {
            camelCaseStr.append(part.substring(0, 1).toUpperCase()).append(part.substring(1).toLowerCase());
        }
        return camelCaseStr.toString();
    }


    /**
     * @Description _将其设置为驼峰命名的形式
     * @author wenjie
     * @date 2024/8/21 15:28
     */
    public static String variable_to_java(String s) {
        return first_small(class_to_java(s));
    }

    /**
     * @Description 获取当前的时间 转为 yyyy-MM-dd HH:mm:ss
     * @author wenjie
     * @date 2024/8/21 15:41
     */
    public static String get_time() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        return formattedDate;
    }

    /**
     * @Description 获取秒级时间戳
     * @author wenjie
     * @date 2024/8/21 15:43
     */
    public static String get_timestamp_by_m(String s) {
        long time = new Date().getTime() / 1000;
        return String.valueOf(time);
    }


    /**
     * @Description 获取毫秒级时间戳
     * @author wenjie
     * @date 2024/8/21 15:44
     */
    public static String get_timestamp_by_ms(String s) {
        long time = new Date().getTime();
        return String.valueOf(time);
    }


}
